﻿using System;
using System.ComponentModel;

namespace HalconDotNet
{
	// Token: 0x0200008B RID: 139
	public class HObjectBase : IDisposable
	{
		// Token: 0x06001E1F RID: 7711 RVA: 0x00045740 File Offset: 0x00043940
		internal HObjectBase() : this(HObjectBase.UNDEF, false)
		{
		}

		// Token: 0x06001E20 RID: 7712 RVA: 0x000B58C0 File Offset: 0x000B3AC0
		internal HObjectBase(IntPtr key, bool copy)
		{
			if (copy && key != HObjectBase.UNDEF && key != HObjectBase.UNDEF2)
			{
				this.key = HalconAPI.CopyObject(key);
				return;
			}
			this.key = ((key == HObjectBase.UNDEF2) ? HObjectBase.UNDEF : key);
		}

		// Token: 0x06001E21 RID: 7713 RVA: 0x000B5923 File Offset: 0x000B3B23
		internal HObjectBase(HObjectBase obj) : this(obj.key, true)
		{
			GC.KeepAlive(obj);
		}

		/// <summary>Returns the HALCON ID for this iconic object</summary>
		/// <remarks>Caller must ensure that object is kept alive</remarks>
		// Token: 0x1700006F RID: 111
		// (get) Token: 0x06001E22 RID: 7714 RVA: 0x000B5938 File Offset: 0x000B3B38
		[EditorBrowsable(EditorBrowsableState.Never)]
		public IntPtr Key
		{
			get
			{
				return this.key;
			}
		}

		/// <summary>
		///   Returns true if the iconic object has been initialized.
		/// </summary>
		/// <remarks>
		///   An object will be uninitialized when creating it with a
		///   no-argument constructor or after calling Dispose();
		/// </remarks>
		// Token: 0x06001E23 RID: 7715 RVA: 0x000B5940 File Offset: 0x000B3B40
		public bool IsInitialized()
		{
			return this.key != HObjectBase.UNDEF;
		}

		/// <summary>
		///   Returns a new HALCON ID referencing this iconic object, which will
		///   remain valid even after this object is disposed (and vice versa).
		///   This is only useful if the ID shall be used in another language
		///   interface (in fact, the key needs to be externally disposed,
		///   a feature not even offered by the .NET language interface).
		/// </summary>
		// Token: 0x06001E24 RID: 7716 RVA: 0x000B5952 File Offset: 0x000B3B52
		[EditorBrowsable(EditorBrowsableState.Never)]
		public IntPtr CopyKey()
		{
			IntPtr result = HalconAPI.CopyObject(this.key);
			GC.KeepAlive(this);
			return result;
		}

		// Token: 0x06001E25 RID: 7717 RVA: 0x000B5965 File Offset: 0x000B3B65
		[EditorBrowsable(EditorBrowsableState.Never)]
		public void TransferOwnership(HObjectBase source)
		{
			if (source == this)
			{
				return;
			}
			this.Dispose();
			if (source == null)
			{
				return;
			}
			this.key = source.key;
			source.key = HObjectBase.UNDEF;
			this.suppressedFinalization = false;
			GC.ReRegisterForFinalize(this);
		}

		// Token: 0x06001E26 RID: 7718 RVA: 0x000B599C File Offset: 0x000B3B9C
		~HObjectBase()
		{
			try
			{
				this.Dispose(false);
			}
			catch (Exception)
			{
			}
		}

		// Token: 0x06001E27 RID: 7719 RVA: 0x000B59D8 File Offset: 0x000B3BD8
		private void Dispose(bool disposing)
		{
			if (this.key != HObjectBase.UNDEF)
			{
				HalconAPI.ClearObject(this.key);
				this.key = HObjectBase.UNDEF;
			}
			if (disposing)
			{
				GC.SuppressFinalize(this);
				this.suppressedFinalization = true;
			}
			GC.KeepAlive(this);
		}

		// Token: 0x06001E28 RID: 7720 RVA: 0x000B5A18 File Offset: 0x000B3C18
		void IDisposable.Dispose()
		{
			this.Dispose(true);
		}

		/// <summary>Releases the resources used by this tool object</summary>
		// Token: 0x06001E29 RID: 7721 RVA: 0x000B5A18 File Offset: 0x000B3C18
		public virtual void Dispose()
		{
			this.Dispose(true);
		}

		// Token: 0x06001E2A RID: 7722 RVA: 0x000B5A21 File Offset: 0x000B3C21
		internal void Store(IntPtr proc, int parIndex)
		{
			HalconAPI.HCkP(proc, HalconAPI.SetInputObject(proc, parIndex, this.key));
		}

		// Token: 0x06001E2B RID: 7723 RVA: 0x000B5A38 File Offset: 0x000B3C38
		internal int Load(IntPtr proc, int parIndex, int err)
		{
			if (this.key != HObjectBase.UNDEF)
			{
				throw new HalconException("Undisposed object instance when loading output parameter");
			}
			if (HalconAPI.IsFailure(err))
			{
				return err;
			}
			err = HalconAPI.GetOutputObject(proc, parIndex, out this.key);
			if (this.suppressedFinalization)
			{
				this.suppressedFinalization = false;
				GC.ReRegisterForFinalize(this);
			}
			return err;
		}

		/// <summary>Represents an uninitialized HALCON object key</summary>
		// Token: 0x0400115E RID: 4446
		[EditorBrowsable(EditorBrowsableState.Never)]
		public static readonly IntPtr UNDEF = IntPtr.Zero;

		// Token: 0x0400115F RID: 4447
		internal static readonly IntPtr UNDEF2 = new IntPtr(1);

		// Token: 0x04001160 RID: 4448
		internal IntPtr key = HObjectBase.UNDEF;

		// Token: 0x04001161 RID: 4449
		private bool suppressedFinalization;
	}
}
